package com.wlyuan.index.application.service;

import com.alibaba.fastjson.JSON;
import com.wlyuan.index.api.ITestService;
import com.wlyuan.index.domain.index.WaybillIndex;
import com.wlyuan.index.domain.index.repository.WaybillIndexRepository;
import com.wlyuan.index.domain.source.factory.SourceFactory;
import com.wlyuan.index.domain.source.repository.SourceRepository;
import com.wlyuan.index.remote.consumer.*;
import com.wlyuan.index.remote.message.*;
import com.wlyuan.index.remote.store.MessageStore;
import com.wlyuan.tools.Result;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.PostConstruct;

@DubboService
@Slf4j
@RequiredArgsConstructor
public class TestServiceImpl implements ITestService {

    private final MessageStore messageStore;
    private final WaybillIndexRepository waybillIndexRepository;
    private final QuotationMessageConsumer quotationMessageConsumer;
    private final BillMessageConsumer billMessageConsumer;
    private final BalanceMessageConsumer balanceMessageConsumer;
    private final WaybillMessageConsumer waybillMessageConsumer;
    private final CombineMessageConsumer combineMessageConsumer;
    private final SourceRepository sourceRepository;
    private final SourceFactory sourceFactory;

    private final WaybillIndex waybillIndex = new WaybillIndex();

    @PostConstruct
    public void initialize() {
        waybillIndex.setWaybillId(1L);
    }

    @Override
    public void concurrency() {
    }

    @Override
    public Result<String> concurrency(String message) {
        return Result.success(message);
    }

    @Override
    public void writeES() {
        waybillIndexRepository.save(waybillIndex);
    }

    @Override
    public void consume(String messageClass) {
        if (QuotationMessage.class.getSimpleName().equals(messageClass)) {
            var messages = messageStore.list(QuotationMessage.class);
            messages.forEach(quotationMessageConsumer::consume);
        } else if (BillMessage.class.getSimpleName().equals(messageClass)) {
            var messages = messageStore.list(BillMessage.class);
            messages.forEach(billMessageConsumer::consume);
        } else if (BalanceMessage.class.getSimpleName().equals(messageClass)) {
            var messages = messageStore.list(BalanceMessage.class);
            messages.forEach(balanceMessageConsumer::consume);
        } else if (WaybillMessage.class.getSimpleName().equals(messageClass)) {
            var messages = messageStore.list(WaybillMessage.class);
            messages.forEach(waybillMessageConsumer::consume);
        } else if (CombineMessage.class.getSimpleName().equals(messageClass)) {
            var messages = messageStore.list(CombineMessage.class);
            messages.forEach(combineMessageConsumer::consume);
        }
    }

    @Override
    public void quotation() {
        var string = "{\"args\":{\"quotationId\":553024,\"tenantId\":1,\"userId\":151},\"argsClass\":\"com.wlyuan.quotation.dto.quotation.QuotationAgreeDTONew\",\"event\":\"QuotationAgreed\",\"offline\":false,\"own\":false,\"quotation\":{\"arrivePayFee\":null,\"calculateVolume\":153.22,\"calculateWeight\":274.97,\"collectionFee\":null,\"createBy\":151,\"deliverFee\":333.00,\"hasBargain\":false,\"hasSurcharge\":false,\"insureFee\":0.00,\"loadFee\":551.59,\"matchId\":132,\"pickFee\":12.00,\"prepayFee\":null,\"quotationId\":553024,\"quotationNo\":\"DOC687000895488\",\"relations\":[{\"status\":2,\"tenantId\":15},{\"status\":2,\"tenantId\":1}],\"sourceId\":254319,\"sourceType\":1,\"status\":2,\"statusName\":\"已同意\",\"stubId\":10745,\"subjectId\":10745,\"subjectSourceId\":254319,\"subjectType\":1,\"surchargeFees\":[],\"taxFee\":0.00,\"tenantId\":1,\"totalFee\":2352.18,\"totalQuantity\":123,\"transportFee\":1455.59,\"transportMode\":1,\"unloadFee\":0.00,\"version\":1704682916814},\"tenantId\":1,\"time\":\"2024-01-08T11:01:56.823\",\"toTenantId\":15,\"userId\":151}";
        var message = JSON.parseObject(string, QuotationMessage.class);
        message.getQuotation().setVersion(System.currentTimeMillis());
//        quotationMessageConsumer.consume(message);
    }
}
